import {
  getCategoryListFun,
  getVenueListFun,
  getCoachListFun,
  courseTemplateListFun,
  vipCardListFun,
  addClassFun,
} from "./services";
import { namespace } from "./configFile";
import { message } from "antd";
export default {
  namespace,
  state: {
    categoryList: [],
    venueList: [],
    coachList: [],
    courseTemplateList: [],
    vipCardList: [],
  },
  effects: {
    //课程分类
    *getCategoryList({ params }, { call, put }) {
      const request = yield call(getCategoryListFun, {
        //courseStatus: 0,
      });
      let objList = [];
      request &&
        request.length > 0 &&
        request.map((item) =>
          objList.push({
            ...item,
            disabled: item.courseStatus == 0 ? false : true,
          })
        );
      yield put({
        type: "save",
        payload: {
          categoryList: objList,
        },
      });
    },
    //所有场馆
    *getVenueList({ params }, { call, put }) {
      const request = yield call(getVenueListFun);
      yield put({
        type: "save",
        payload: {
          venueList: request,
        },
      });
    },
    //所有教练
    *getCoachList({ params }, { call, put }) {
      const request = yield call(getCoachListFun, params);
      yield put({
        type: "save",
        payload: {
          coachList: request,
        },
      });
    },
    //约课流程
    *getCourseTemplateList({ params }, { call, put }) {
      const request = yield call(courseTemplateListFun, params);
      yield put({
        type: "save",
        payload: {
          courseTemplateList: request,
        },
      });
    },
    //会员卡
    *getVipCardList({ params, callback }, { call, put }) {
      const request = yield call(vipCardListFun, params);
      callback && callback();
      yield put({
        type: "save",
        payload: {
          vipCardList: request.records,
        },
      });
    },
    //新增课程
    *submit({ params, callback }, { call, put }) {
      const response = yield call(addClassFun, params);
      if (response.code === 200) {
        message.success("保存成功");
        callback && callback(1);
      } else {
        callback && callback(0);
        message.error(response.msg);
      }
    },
  },
  reducers: {
    save(state: any, { payload }: any) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
